<?php
/**
 * Created by PhpStorm.
 * User: bevan
 * Date: 2018/1/3
 * Time: 下午7:56
 */

namespace app\admin\service;

use thans\auth\model\AuthGroupAccessModel;
use thans\auth\model\AuthRuleModel;
use thans\auth\model\AuthGroupModel;
use thans\auth\facade\Auth;
use think\Exception;

class GroupService
{
    /**
     *
     * @method 获取所有的权限组
     * @version
     * @User: bevan
     * @return
     */
    public function allList($keyword ='')
    {

        $groups = Auth::getAllGroups($keyword);

        return ['code' => 1001, 'msg' => '获取成功', 'data' => $groups];
    }

    /**
     *
     * @method 创建组 post
     * @version
     * @User: bevan
     * @return
     */
    public function save($data)
    {
        if(is_array($data['rules'])){
            $data['rules'] = implode(',', $data['rules']);
        }

        try {
            AuthGroupModel::create($data, true);
        } catch (Exception $e) {
            return ['code' => 4001, 'msg' => '创建失败,' . $e->getMessage(), 'data' => []];
        }

        return ['code' => 1001, 'msg' => '创建成功', 'data' => []];
    }

    /**
     *
     * @method 编辑组 获取组信息
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function read($id)
    {
        $group = AuthGroupModel::get($id);

        return ['code' => 1001, 'msg' => '获取成功', 'data' => $group];
    }

    /**
     *
     * @method 更新组信息
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function update($id, $data)
    {
        $group = new AuthGroupModel();
        if(is_array($data['rules'])){
            $data['rules'] = implode(',', $data['rules']);
        }
        try {
            $group->allowField(true)->save($data, ['id', $id]);
        } catch (Exception $e) {
            return ['code' => 4001, 'msg' => '更新失败,' . $e->getMessage(), 'data' => []];
        }

        return ['code' => 1001, 'msg' => '更新成功', 'data' => []];
    }

    /**
     *
     * @method 删除组
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function delete($id)
    {
        //判断是否是admin管理员
        if ((int)$id === 1) {
            return ['code' => 4001, 'msg' => '不允许删除超级管理员', 'data' => []];
        }
        //首先验证该组下面是否有成员 , 若有成员不允许删除
        $count_num = AuthGroupAccessModel::where('group_id', $id)->count();

        if ($count_num) {
            return ['code' => 4001, 'msg' => '该组下有成员,不允许删除', 'data' => []];
        }
        try {
            AuthGroupModel::destroy($id);
        } catch (Exception $e) {
            return ['code' => 4001, 'msg' => '删除失败,' . $e->getMessage(), 'data' => []];
        }

        return ['code' => 1001, 'msg' => '删除成功', 'data' => []];
    }

    /**
     *
     * @method 获取该组权限
     * @version
     * @User: bevan
     * @param $id 组id
     * @return
     */
    public function getAuth($id = 0)
    {
        $menuService = new MenuService();

        return $menuService->getTreeList($id); //树状层级的所有菜单
    }

    /**
     *
     * @method 切换组（角色）的状态
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function toggleGroupStatus($id)
    {
        if($id == 1){
            return ['code'=>4001,'msg'=>'禁止修改超级管理组状态','data'=>[]];
        }
        $group = AuthGroupModel::get($id);

        if($group['status']){
            $group->status = 0;
        }else{
            $group->status = 1;
        }

        $group->save();

        return ['code'=>1001,'msg'=>'更新成功','data'=>[]];
    }
}